1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email transport capabilities.
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8 package org.webdocwf.util.smime.test;
9
10
11 import javax.mail.Transport;
12 import org.webdocwf.util.smime.smime.EnvelopedSMIME;
13 import org.webdocwf.util.smime.exception.SMIMEException;
14 import java.io.File;
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17
18
19 /***
20 * Tests enveloping process. Encrypted text/html message with
21 * attachments can be sent by this test. This example is test for composing
22 * of email message content by html code which is generated by application, and
23 * adding resources assocated in html code from the application, and from the
24 * file system. Also, attachments are added to message from the application,
25 * or from file system. To get help for this example type:
26 * "java org.webdocwf.util.smime.test.TestEncryptGeneratedHtml" in command line.
27 * It is assumed that smime200tests.jar is in your classpath.<BR>
28 * <BR>
29 * Parameters passed to example are:<BR>
30 * <mailHost> <mailAddress> <cerFileName> <algorithmName>
31 * [<attachment>]<BR>
32 * <BR>
33 * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
34 * <BR>
35 * Note that for this example email address "FROM" is fixed to: "sender@seateam.co.yu".
36 * You should change this values in source code of TestEncryptGeneratedHtml.java
37 * in order to use them with other "FROM" address.
38 */
39 public class TestEncryptGeneratedHtml {
40
41 public static void main(String[] args) {
42
43 String subject = "S/MIME encrypted message - Subject test: ÜüÄäÖöÜüß";
44 String from = "sender@seateam.co.yu";
45
46 if (args.length < 4) {
47 System.err.println(
48 System.getProperty("line.separator") +
49 "Usage of TestEncryptGeneratedHtml: " +
50 System.getProperty("line.separator") +
51 "java TestEncryptGeneratedHtml <mailHost> <mailAddress> <cerFileName> " +
52 "<algorithmName> [<attachment>]" +
53 System.getProperty("line.separator") +
54 System.getProperty("line.separator") +
55 "Examples:" +
56 System.getProperty("line.separator") +
57 "java TestEncryptGeneratedHtml seateam.co.yu recipient@seateam.co.yu " +
58 "recipient512.cer RC240 " +
59 System.getProperty("line.separator") +
60 "java TestEncryptGeneratedHtml seateam.co.yu recipient@seateam.co.yu " +
61 "recipient512.cer DES .//test//Zip8Test.zip");
62 System.exit(-1);
63 }
64 String smtpHost = args[0];
65 String addressTO = args[1];
66 String cerFileName = args[2];
67 String algorithmName = args[3];
68 String fileName = null;
69
70 if (args.length > 4)
71 fileName = args[4];
72
73 String addressCC = "recipient@seateam.co.yu";
74 String addressBCC = "recipient@seateam.co.yu";
75
76 subject = algorithmName + " " + cerFileName + " " + subject;
77
78 EnvelopedSMIME es = null;
79
80 try {
81 ExampleGenerator generator = new ExampleGenerator();
82
83 // Address for resource green.gif which will be obtained by program generation
84 // from instances of the ExampleGenerator class as InputSteam must be added to
85 // html code as virtual. For more information refer to setContent method of
86 // SignedSMIME class.
87 String green = "*****000generated.gif";
88
89 // Address for resource red.gif class will be defined as absolute address to
90 // location in the file system.
91 File redFile = new File("./test/pictures/red.gif");
92 String red = redFile.getAbsoluteFile().getCanonicalPath();
93
94 // Address for resource blue.gif class will be defined as file type of url
95 // address.
96 File blueFile = new File("./test/pictures/blue.gif");
97 String blue = "file:///" + blueFile.getAbsoluteFile().getCanonicalPath();
98
99 blue = blue.replace('//', '/');
100
101 // Addresses for resources orange.jpg and yellow.jpg are left relative.
102 // Default values of resources in ExampleGenerator are relative, as it can be
103 // seen in the original file of html example HtmlTest.html. To resolve this
104 // relative adresses common directory path must be obtained from the root.
105 // For more information refer to setContent method of
106 // SignedSMIME class.
107 String commonPath = new File("./test/HtmlTest.html").getParent();
108
109 generator.setResourceInExampleHtml("GREEN", green);
110 generator.setResourceInExampleHtml("RED", red);
111 generator.setResourceInExampleHtml("BLUE", blue);
112
113 // Simulation of resources (green.gif) obtained by program.
114 InputStream[] in = {generator.getGifImage()};
115
116 // Generation of signed smime object
117 es = new EnvelopedSMIME(smtpHost, from, subject);
118 // es.setContent( generator.getHtmlStream(), "text/html", commonPath, in );
119 es.setContent(generator.getHtmlString(), "text/html", commonPath, in);
120
121 // Attachments can be added to message from program (using Input stream). In
122 // the following line zip file will be generated and added as attachment. You must
123 // define some file names in order to give appropriate mime-type to your message.
124 es.addAttachment(generator.getZipStream(), "test.zip");
125
126 // File can also be added from file system
127 File attachment2 = new File("./test/AdobeAcrobatTest.pdf");
128
129 es.addAttachment(attachment2);
130 File attachment3 = new File("./test/Word2000Test.doc");
131
132 es.addAttachment(attachment3);
133
134 if (fileName != null) {
135 es.addAttachment(fileName); // optional - use this if send attachment
136 }
137
138 es.setReply(from); // optional
139
140 es.addRecipient(addressTO, "TO", cerFileName); // mandatory
141 // es.addRecipient(addressCC, "CC", cerFileName); // optional
142 // es.addRecipient(addressBCC, "BCC", cerFileName); // optional
143
144 if (algorithmName.equals("RC240")) {
145 System.out.println("Creating the encrypted message with RC2_CBC - 40 bits algorithm... ");
146 es.enveloping(); // instead of this next line could be used
147 // es.enveloping("RC2_CBC", 40);
148 } else if (algorithmName.equals("RC264")) {
149 System.out.println("Creating the encrypted message with RC2_CBC - 64 bits algorithm... ");
150 es.enveloping("RC2_CBC", 64); // send message with RC2 - 64 bits algorithm
151 } else if (algorithmName.equals("RC2128")) {
152 System.out.println("Creating the encrypted message with RC2_CBC - 128 bits algorithm... ");
153 es.enveloping("RC2_CBC", 128); // send message with RC2 - 128 bits algorithm
154 } else if (algorithmName.equals("DES")) {
155 System.out.println("Creating the encrypted message with DES algorithm... ");
156 es.enveloping("DES", 56); // send message with DES algorithm
157 } else if (algorithmName.equals("3DES")) {
158 System.out.println("Creating the encrypted message with DES_EDE3_CBC algorithm... ");
159 es.enveloping("DES_EDE3_CBC", 192); // send message with 3DES algorithm
160 }
161 System.out.print("Sending encrypted message ... ");
162 es.send(); // instead of this next line could be used
163 // Transport.send(es.getSignedMessage());
164 System.out.println("done.");
165
166 } catch (Exception e) {
167 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
168 if (e instanceof SMIMEException) {
169 // ((SMIMEException)e).loggingErrors(null); //logging
170 ((SMIMEException) e).displayErrors(null);
171 } else
172 System.out.println(e.getMessage());
173 }
174 }
175
176 }
177
This page was automatically generated by Maven